
#include "main.h"

uint8_t gDATABUF[DATA_BUF_SIZE];
///////////////////////////////////
// Default Network Configuration //
///////////////////////////////////
wiz_NetInfo gWIZNETINFO = {
    .mac={0x00, 0x08, 0xdc, 0x00, 0xab, 0xcd},
    .ip={192, 168, 1, 123},
    .sn={255,255,255,0},
    .gw={192, 168, 1, 1},
    .dns={8,8,8,8},
    .dhcp=NETINFO_STATIC };


wiz_NetInfo gWIZNETINFO_chk={0};
uint8 PHY_connect=0;


/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/


void  wizchip_select(void)
{
	//GPIO_ResetBits(GPIOA, WIZ_SCS);
}

void  wizchip_deselect(void)
{
	//GPIO_SetBits(GPIOA, WIZ_SCS);
}

void  wizchip_write(uint8_t wb)
{
	//SPI_SendByte(wb);
	LPSPI1_transmit_8bits(wb);
}

uint8_t wizchip_read()
{
	//return SPI_ReceiveByte();
	return LPSPI1_receive_8bits();
}

void wizchip_port_init (void) 
{
    //wizchip w5500 rst
    BSW_GPIO_MUX_CONFIG(Bsw_Port_D,WIZ_RSTn,PORT_MUX_AS_GPIO);    
    BSW_GPIO_Direction(PTD,WIZ_RSTn,1);//输出

    //wizchip w5500 INT
    BSW_GPIO_MUX_CONFIG(Bsw_Port_D,WIZ_INTn,PORT_MUX_AS_GPIO);
    BSW_GPIO_Direction(PTE,WIZ_INTn,0);//输入
}


/**
*@brief		W5500复位设置函数
*@param		无
*@return	无
*/
void wizchip_reset(void)
{
  BSW_GPIO_ResetBits(PTD,WIZ_RSTn);
  //delay_us(500);
  delay_ms(1);
  BSW_GPIO_SetBits(PTD,WIZ_RSTn);
  //delay_us(500);
  delay_ms(5);
}


int network_init(void)
{
   uint8_t tmpstr[6];

    //ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
    ctlwizchip(CW_GET_ID,(void*)tmpstr);
    ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
    ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO_chk);
    if (memcmp(&gWIZNETINFO,&gWIZNETINFO_chk,sizeof(gWIZNETINFO))!=0)
        return 1;
    return 0;
}
/////////////////////////////////////////////////////////////
int wizchip_Hard_INIT(void)
{
    wizchip_port_init();
    ECU_W5500_SPI_INIT();

    reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect);
    /* SPI read & write callback function */
    reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write);

    //复位(先低再高) 50ms
    wizchip_reset();
    /* PHY link status check */
    //wizchip_phy_check();

    return 0;
}

/////////////////////////////////////////////////////////////
int wizchip_Net_INIT(void)
{
    uint8_t memsize[2][8] = {{2,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,2}};// tx/rx buffer set K bytes
    uint8_t memsize_chk[2][8] = {{0}, {0}};// tx/rx buffer set K bytes

    /* Network Initialization */
    if(network_init()!=0)
        return 1;

    /* WIZCHIP SOCKET Buffer initialize */
    if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1)
        return 2;
    
    ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize);
    for(int i=0;i<8;i++){
        memsize_chk[0][i]=getSn_TXBUF_SIZE(i);
    }
    for(int i=0;i<8;i++){
        memsize_chk[1][i]=getSn_RXBUF_SIZE(i);
    }
    if(memcmp(memsize,memsize_chk,sizeof(memsize))!=0)
        return 3;
    return 0;
}


typedef enum{
    WIZ_Idle=0,
    WIZ_HardInit,
    WIZ_NetInit,
    WIZ_RUN,
}EN_WIZ_STATE;

EN_WIZ_STATE G_enum_Wiz_state=WIZ_HardInit;
u8 G_u8_Wiz_errcode;

/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
void wizchip_Main(void)
{
    uint8_t sn = 0;
    int32_t ret = 0;


    if(G_enum_Wiz_state==WIZ_HardInit)
    {
        wizchip_Hard_INIT();
        G_enum_Wiz_state=WIZ_NetInit;
    }
    else if(G_enum_Wiz_state==WIZ_NetInit)
    {
        G_u8_Wiz_errcode=wizchip_Net_INIT();
        if(G_u8_Wiz_errcode==0)
            G_enum_Wiz_state=WIZ_RUN;
    }
    else if(G_enum_Wiz_state==WIZ_RUN)
    {
        ctlwizchip(CW_GET_PHYLINK, (void*)&PHY_connect);
        //Link down
        if(PHY_connect==0)
        {

        }
        //Link up
        else
        {
            if((ret = loopback_tcps(sn, gDATABUF, 3000)) < 0)
            {
                //break;
            }
            //W5200支持主动ping，W5500不支持主动ping
            //do_ping();
#if 0
            if((ret = loopback_tcps(sn+1, gDATABUF, 3000)) < 0)
            {
                //break;
            }
#endif

        }
    }

    return;

#if 0
    if((ret = rcvonly_tcps(sn, gDATABUF, 3000)) < 0)
    {
        break;
    }
 #endif
}



